/*
 * ADC.c
 *
 * Created: 08.02.2013 17:22:41
 *  Author: saenko
 */ 
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/delay.h>



unsigned char ADC_measure(void)
{
	ADCSRA |= _BV(ADSC);               // Start conversion

	while(!bit_is_set(ADCSRA,ADIF));   // Loop until conversion is complete

	ADCSRA |= _BV(ADIF);
	
	return ADCH;
	
}

void ADC_init(unsigned char channel,unsigned char ref_volt)
{
	ADMUX = channel; // channel == ADC_PIN or channel == 0b1110 -> 1.1V IN or channel == 1111 -> 0V IN	
	ADMUX |= 0b00100000; // ADLAR
	
	switch (ref_volt)
	{
		case 0: break;						// AREF
		case 1: ADMUX |= 0b01000000; break; // AVcc
		case 3: ADMUX |= 0b11000000; break; // 1.1V
	}
	
	if(channel < 6)	DIDR0 = (1<<channel);
	
	ADCSRA = (1<<ADEN)|(1<<ADSC)|(1<<ADPS2)|(1<<ADPS1);	
	
	ADC_measure();
	
}



